# -*- coding: utf-8 -*-
"""
Created on Sat Dec  5 20:08:08 2020

@author: Team317
"""

import os
import cv2

root = '../Image/'
save_root = '../age_image/train1'
folders = os.listdir(root)
count = [0 for i in range(10)]
# 根据文件名对图片进行分类
for folder in folders:
    folder_path = root + folder + '/'
    folder1 = os.listdir(folder_path)
    
    # 为10个年龄段的图片分别创建文件夹
    for i in range(10):
        # 没有文件夹则创建文件夹
        if not os.path.exists(save_root + '/' + folder):
            os.mkdir(save_root + '/' + folder)
        # 保存图片的文件夹路径
        pt = save_root + '/' + folder + '/Age_' + str(i)  
        if not os.path.exists(pt):
            os.mkdir(pt)
        
    
    for file in folder1:

        # 获取标签
        if file[3] != '_':
            label = int('10') -1
            
        else:
            label = int(file[2]) - 1
        
        # 生成一个小批量的数据集
        count[label] += 1
        if count[label] <= 1000:
            
            # 根据路径读取图片
            image_path= folder_path + file
            image = cv2.imread(image_path)
            # 缩放图片为统一大小
            image = cv2.resize(image, (112,112))

            #保存图片到分类文件夹
            save_path = save_root + '/' + folder + '/Age_' + str(label) + '/' + file
            cv2.imwrite(save_path, image)
            
